// 通用返回数据结构
import { utils, XCommonRet } from 'xmcommon';

export interface IHttpRet {
  code: number;
  msg?: string;
  data?: unknown;
  path?: string;
  timestamp?: string;
}

export class RetUtils {
  /**
   * 通用返回数据结构
   * @param code 状态码
   * @param msg 状态信息
   * @param data 数据
   * @param path 路径
   */
  static ret(
    code: number,
    msg?: string,
    data?: unknown,
    path?: string,
  ): IHttpRet {
    return {
      code,
      msg,
      data,
      path,
      timestamp: new Date().toISOString(),
    };
  }

  public static byCommonRet(paramRet: XCommonRet, path: string): IHttpRet {
    const data = utils.isNull(paramRet.data) ? undefined : paramRet.data;
    return {
      code: paramRet.err,
      msg: paramRet.msg,
      data,
      path,
      timestamp: new Date().toISOString(),
    };
  }
}
